package com.esp.soccertutor.ui.xmlparser;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.esp.soccertutor.ui.bean.BeanProducts;
import com.esp.soccertutor.util.Const;
import com.esp.soccertutor.util.Log;
import com.esp.soccertutor.util.Utils;

public class XMLParser_SCProducts {
	
	public String TAG = XMLParser_SCProducts.class.getCanonicalName();
	private String strServerXML;
	private static Document docShowcaseProducts;
	public static int STATUS_CODE;
	public static int RESPONSE_CODE = -1;
	public static int NODE_COUNT;
	public static String MESSAGE;
	public String contentFrom;
	
	private ArrayList<BeanProducts> arrylstProducts;
	private ArrayList<BeanProducts> arrylstDetails;
	
	public XMLParser_SCProducts(String wsURL){
		
		try
		{
			XMLDocument.getHttpGetXML(wsURL);
			strServerXML = Utils.readTxt(Const.CONTEXT,"Product.txt");

			if(XMLDocument.HTTP_XML_STATUS == 200)
			{
				Log.print(TAG,strServerXML);
				docShowcaseProducts = XMLDocument.XMLfromString(strServerXML.replaceAll("&","&amp;").trim());
				STATUS_CODE = 200;
				
			}
			else if(XMLDocument.HTTP_XML_STATUS == 401)
			{
				// XML not validating 
				STATUS_CODE = 401;
			}
			else if(XMLDocument.HTTP_XML_STATUS == 500)
			{
				// 	internal error occurred								
				STATUS_CODE = 500;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			STATUS_CODE = 500;
		}
		
	}
	
	public static Document GetDocument(){
		
		if(docShowcaseProducts != null){
			return docShowcaseProducts;
		}else{
			return null;
		}
	}
	
	public ArrayList<BeanProducts> getShowcaseProducts(Document docCategory){
		try
		{
			if(docCategory != null)
				return docBuild(docCategory);
			else
				return null;
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	private ArrayList<BeanProducts> docBuild(Document doc) throws Exception{
		
		if(doc != null)
		{
			
			
			Element elem$root = doc.getDocumentElement();
			NodeList nl$item = elem$root.getElementsByTagName("code");
			Element  line  = (Element)nl$item.item(0);
			RESPONSE_CODE = Integer.parseInt(XMLDocument.getCharacterDataFromElement(line, "code"));
			
			NodeList nodeMessage = elem$root.getElementsByTagName("message");
			line  = (Element)nodeMessage.item(0);
			MESSAGE = XMLDocument.getCharacterDataFromElement(line, "message");
			
			NodeList nodeCount = elem$root.getElementsByTagName("count");
			line = (Element)nodeCount.item(0);
			NODE_COUNT = Integer.parseInt(XMLDocument.getCharacterDataFromElement(line,"count"));
				
			
			arrylstDetails = new ArrayList<BeanProducts>();
			BeanProducts mDetails = new BeanProducts();
			
			NodeList nodeFrom = elem$root.getElementsByTagName("contentFrom");
			line = (Element)nodeFrom.item(0);
			mDetails.setContentFrom(XMLDocument.getCharacterDataFromElement(line,"contentFrom"));
			
			NodeList nodeCounts = elem$root.getElementsByTagName("contentCounts");
			line = (Element)nodeCounts.item(0);
			mDetails.setContentCounts(XMLDocument.getCharacterDataFromElement(line,"contentCounts"));
			
			NodeList nodePageNo = elem$root.getElementsByTagName("pageNo");
			line = (Element)nodePageNo.item(0);
			mDetails.setPageNo(XMLDocument.getCharacterDataFromElement(line,"pageNo"));
			
			NodeList nodePageCounts = elem$root.getElementsByTagName("pageCounts");
			line = (Element)nodePageCounts.item(0);
			mDetails.setPageCounts(XMLDocument.getCharacterDataFromElement(line,"pageCounts"));
			
			
			NodeList nodeCategoryName = elem$root.getElementsByTagName("categoryName");
			line = (Element)nodeCategoryName.item(0);
			mDetails.setCategoryName(XMLDocument.getCharacterDataFromElement(line,"categoryName"));
			
			
			arrylstDetails.add(mDetails);
			
			
			NodeList nodeContent = elem$root.getElementsByTagName("content");
			
			if(RESPONSE_CODE == 1)
			{
				if(NODE_COUNT > 0)
				{
					
					arrylstProducts = new ArrayList<BeanProducts>();
					BeanProducts newProduct = new BeanProducts();
					newProduct.mProducts = arrylstDetails;
					arrylstProducts.add(newProduct);
					
					
					for (int i = 0; i < nodeContent.getLength(); i++) 
						{
							
							BeanProducts mProducts = new BeanProducts();
							
							Element elementPos= (Element) nodeContent.item(i);
							
							NodeList nodeID = elementPos.getElementsByTagName("id");
							line  = (Element)nodeID.item(0);
							mProducts.setProductID(XMLDocument.getCharacterDataFromElement(line, "title"));
							
							
							NodeList nodeTitle = elementPos.getElementsByTagName("title");
							line  = (Element)nodeTitle.item(0);
							mProducts.setProductName(XMLDocument.getCharacterDataFromElement(line, "title"));
							
							NodeList nodeImageURL = elementPos.getElementsByTagName("imageURL");
							line  = (Element)nodeImageURL.item(0);
							mProducts.setImageURL(XMLDocument.getCharacterDataFromElement(line, "imageURL"));
							
							
							NodeList nodeDesc = elementPos.getElementsByTagName("desc");
							line  = (Element)nodeDesc.item(0);
							mProducts.setProductDesc(XMLDocument.getCharacterDataFromElement(line, "desc"));
							
							NodeList nodeStorURL = elementPos.getElementsByTagName("storeURL");
							line  = (Element)nodeStorURL.item(0);
							mProducts.setStoreURL(XMLDocument.getCharacterDataFromElement(line, "storeURL"));
							
							NodeList nodePrviewPath = elementPos.getElementsByTagName("previewFile");
							line  = (Element)nodePrviewPath.item(0);
							mProducts.setPreviewFile(XMLDocument.getCharacterDataFromElement(line, "previewFile"));
							
							NodeList nodePriceUSD = elementPos.getElementsByTagName("priceUSD");
							line  = (Element)nodePriceUSD.item(0);
							mProducts.setPriceUSD(XMLDocument.getCharacterDataFromElement(line, "priceUSD"));
							
							NodeList nodePriceGBP = elementPos.getElementsByTagName("priceGBP");
							line  = (Element)nodePriceGBP.item(0);
							mProducts.setPriceGBP(XMLDocument.getCharacterDataFromElement(line, "priceGBP"));
							
							arrylstProducts.add(mProducts);
						
						}
					
				}
					
			}
			
		}
		return arrylstProducts;
	}
	
	
	
}
